package com.space.school.controller;

import com.space.school.domain.User;
import com.space.school.services.UserService;
import com.space.school.services.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "userOperateServlet",value = "/userOperateServlet")
public class UserOperateServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String userId = req.getParameter("userId");
        String operate = req.getParameter("operate");
        int id=0;
        if(userId!=null){
             id=Integer.parseInt(userId);
        }
//       删除操作
        if(operate.equals("del")){
            int num=userService.delete(id);
            if(num>0){
//                跳转页面  userServlet doget方法 Redirect 重定向
                resp.sendRedirect("/userServlet");
            }

        }else if(operate.equals("update")){
//            先把userid 对应的值取出来
            User user=userService.findById(id);
            if(user!=null){
//                往里面防值
                req.setAttribute("user",user);
                req.getRequestDispatcher("updateUserList.jsp").forward(req,resp);
            }

        }else if(operate.equals("status")){
            String status=req.getParameter("status");
            userService.updateStatus(id,status);
            resp.sendRedirect("/userServlet");
        }else if(operate.equals("add")){
            req.getRequestDispatcher("insertUserList.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        页面提取用户id 和密码
        String userId = req.getParameter("userId");
        String userName = req.getParameter("userName");
//        修改页面的修改的新密码
        String newPassWord = req.getParameter("userNewPassword");
        String passWord = req.getParameter("passWord");
        if(userId==null){
//            为新增操作
            User newUser=new User();
            newUser.setUserName(userName);
            newUser.setPassWord(passWord);
            String message=userService.save(newUser);
            if(message.equals("")){
                //            判断是否能注册成功
                resp.sendRedirect("/userServlet");
            }else {
                req.setAttribute("message",message);
                req.getRequestDispatcher("insertUserList.jsp").forward(req,resp);
            }

        }else {
//            修改操作
            User user=new User();
            user.setUserId(Integer.parseInt(userId));
            user.setPassWord(newPassWord);
            boolean difficultyFlag=UserServiceImpl.difficultyPassword(user.getPassWord());
            if(difficultyFlag) {
                //        修改方法
                userService.update(user);
                resp.sendRedirect("/userServlet");
            }else {
//                第一种 不需要跳转页面直接访问后端
//                url 默认的编码规则 ASIC
//                将utf-8 编码格式的字符串转成 url 编码格式的字符串
//                java.net.URLEncoder.encode("密码过于简单","UTF-8")
//                String message=java.net.URLEncoder.encode("密码过于简单","UTF-8");
                resp.sendRedirect("/userOperateServlet?userId="+user.getUserId()+"&operate=update&message="+java.net.URLEncoder.encode("密码过于简单","UTF-8"));
//                第二种
//                req.setAttribute("userOperate_msg","你的密码过于简单");
////               用户的原密码
////               用户的名称
//                user.setPassWord(passWord);
//                user.setUserName(userName);
//                req.setAttribute("user",user);
//                req.getRequestDispatcher("updateUserList.jsp").forward(req,resp);

            }

        }


    }
}
